This is an R Markdown Notebook. When you execute code within the notebook, the results appear beneath the code.

Data was downloaded (by hand, API was not found) from VMI laskentapalvelu (Valtakunnallinen mets??inventaari), and the data was from years 2009-2013. Parameters for the data download: forest area, tree volume, and tree number were searchded for 20-year age groups, areas categorised by the main tree species, seach including both forests and kitumaa (poorly growing areas), and using district levels.

library(tidyverse)
## -- Attaching packages ------------------------------------------------------------------- tidyverse 1.2.1 --
## v ggplot2 3.1.1       v purrr   0.3.2  
## v tibble  2.1.1       v dplyr   0.8.0.1
## v tidyr   0.8.3       v stringr 1.4.0  
## v readr   1.3.1       v forcats 0.4.0
## -- Conflicts ---------------------------------------------------------------------- tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(plotly)
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
library(OpasnetUtils)
## 
## Attaching package: 'OpasnetUtils'
## The following object is masked from 'package:dplyr':
## 
##     combine
# library(devtools)
# install_github("rOpenGov/statfi") # If not installed yet

#library(statfi) statfi is outdated. Use package pxweb instead.

parms <- data.frame(
  File = c(
    "ID1557142456655.csv",
    "ID1557142551382.csv",
    "ID1557142710762.csv"
  ),
  Variable = c("Pintaala","Runkoluku","Tilavuus"),
  Unit= c("km2","1000kpl","1000m3")
)
dat <- data.frame()
for(i in 1:nrow(parms)) {
  tmp <- read.csv(paste0("C:/Users/jtue/AppData/Local/Temp/Data-",parms$File[i]),
                header=TRUE, sep=";", skip=3,encoding = "UTF-8")
  colnames(tmp) <- c("Maakunta","Ikaluokka","Paapuulaji","Tulos","Keskivirhe","Suht_keskivirhe")
  for(j in 4:6) {tmp[[j]][is.na(tmp[[j]])] <- 0}
  dat <- rbind(dat, cbind(tmp, parms[i, 2:3]))
}  
## Warning in data.frame(..., check.names = FALSE): row names were found from
## a short variable and have been discarded

## Warning in data.frame(..., check.names = FALSE): row names were found from
## a short variable and have been discarded

## Warning in data.frame(..., check.names = FALSE): row names were found from
## a short variable and have been discarded
dat$Ikaluokka <- factor(dat$Ikaluokka, levels=c("Puuton","1-20","21-40","41-60","61-80","81-100",
                                                "101-120","121-140","141-160","161+"))
dat$Maakunta <- factor(dat$Maakunta, levels=unique(dat$Maakunta))
dat$Alue <- ifelse(as.numeric(dat$Maakunta) %in% 15:18, "Pohjois-Suomi", "Etel??-Suomi")

ggplot(dat[dat$Variable=="Tilavuus" , ], aes(x=Ikaluokka, weight=Tulos, fill=Maakunta))+geom_bar()+
  facet_grid(.~Alue)

tmp <- dat[dat$Variable=="Tilavuus",]
tmp <- aggregate(tmp["Tulos"], tmp[c("Alue","Ikaluokka")], sum)
pl <- tmp %>%
#  group_by(Alue) %>%
  plot_ly(x=~Ikaluokka, y=~Tulos/1000, color=~Alue, type="bar") %>%
  layout(
#    barmode="group",
    yaxis=list(title="Puuston tilavuus (M m3)")
  )
pl
## Warning in RColorBrewer::brewer.pal(N, "Set2"): minimal value for n is 3, returning requested palette with 3 different levels

## Warning in RColorBrewer::brewer.pal(N, "Set2"): minimal value for n is 3, returning requested palette with 3 different levels
# pushIndicatorGraph(pl, 124) # Does not work due to an unknown error

Add a new chunk by clicking the Insert Chunk button on the toolbar or by pressing Ctrl+Alt+I.

When you save the notebook, an HTML file containing the code and output will be saved alongside it (click the Preview button or press Ctrl+Shift+K to preview the HTML file).

The preview shows you a rendered HTML copy of the contents of the editor. Consequently, unlike Knit, Preview does not run any R code chunks. Instead, the output of the chunk when it was last run in the editor is displayed.